import { createStore, useStore as useVuexStore, Store } from "vuex"
import { IState, IStoreType } from "./types"
import { IChatGetters } from "./chat/getters"

import { IChatActions } from "./chat/actions"
import { IUserActions } from "./user/actions"
import { ISpiderActions } from "./spider/actions"
import { IListActions } from "./list/actions"
import moduleUser from "./user"
import moduleList from "./list"
import moduleSpider from "./spider"
import moduleChat from "./chat"

const store = createStore<IState>({
  state() {
    return {
      name: "why",
      isCollapse: true,
      isDialogConfim: true //false可以点击 true不能点击
    }
  },
  mutations: {
    //修改侧边来打开收起状态
    changeIsCollapse(state: IState, payload: boolean) {
      console.log("提交了")

      state.isCollapse = payload
    },
    changeDialogConfirm(state: IState, payload: boolean) {
      state.isDialogConfim = payload
    }
  },
  actions: {},
  modules: {
    moduleUser,
    moduleList,
    moduleSpider,
    moduleChat
  }
})

//重构getters
const obj = {} as IChatGetters
for (const k in store.getters) {
  const res = k.split("/")
  // const value = store.getters[k];
  // console.log("k-v", k, res, value);

  if (obj.hasOwnProperty(res[0])) {
    // obj[res[0]][res[1]] = () => k;
    // obj[res[0]][res[1]] = value;
    obj[res[0]][res[1]] = k
  } else {
    Object.defineProperty(obj, res[0], {
      value: {
        // [res[1]]: () => store.getters[k],
        // [res[1]]: () => store.getters[k],
        [res[1]]: k
        // [res[1]]: k
        // [res[1]]: () => k,
      },
      enumerable: true,
      writable: true
    })
  }
}

const objActions = {} as IChatActions &
  IUserActions &
  ISpiderActions &
  IListActions

interface lsActions {
  lsActions: typeof objActions
}
//actions重构
for (const k in (store as any)._actions) {
  const res = k.split("/")
  const value = (store as any)._actions[k]
  // console.log("res", res, (store as any)._actions[k]);
  if (res.length > 1) {
    if (objActions.hasOwnProperty(res[0])) {
      objActions[res[0]][res[1]] = () => k
    } else {
      Object.defineProperty(objActions, res[0], {
        value: {
          [res[1]]: () => {
            return k
          }
        },
        enumerable: true,
        writable: true
      })
    }
  } else if (res.length === 1) {
    objActions[res[0]] = value
  }
}

console.log("obj", obj)

interface lsGetters {
  lsGetters: typeof obj
}

type NStore = Store<IStoreType> & lsGetters & lsActions

export function useStore(): Store<IStoreType> {
  return useVuexStore()
}

export function newUseStore(): NStore {
  const fn = useStore() as any
  fn.lsGetters = obj
  // fn.lsMutations = objMutation;
  fn.lsActions = objActions

  return fn
}

export default store
// import { IChatMutations } from "./chat/mutations";
// const objMutation = {} as any;
// interface lsMutatisons {
//   lsMutatisons: typeof objMutation;
// }
//mutations 暂时补充狗
// for (const k in (store as any)._mutations) {
//   const res = k.split("/");
//   const value = (store as any)._mutations[k];
//   if (res.length > 1) {
//     if (objMutation.hasOwnProperty(res[0])) {
//       objMutation[res[0]][res[1]] = value;
//     } else {
//       Object.defineProperty(objMutation, res[0], {
//         value: {
//           [res[1]]: () => (store as any)._mutations[k],
//         },
//         enumerable: true,
//         writable: true,
//       });
//     }
//   } else if (res.length === 1) {
//     objMutation[res[0]] = value;
//   }
// }
